#include "windowhexconvert.h"
#include "hexutil.h"

WindowHexConvert::WindowHexConvert(QWidget *parent)
    : QMainWindow(parent)
    , isSwaped(false)
{
    setWindowTitle(tr("十六进制字符串转文字"));
    QHBoxLayout *mainLayout = new QHBoxLayout();

    QVBoxLayout *vLayout1 = new QVBoxLayout();
    QVBoxLayout *vLayout2 = new QVBoxLayout();
    QVBoxLayout *vLayout3 = new QVBoxLayout();

    label1 = new QLabel(tr("十六进制字符串"), this);
    label2 = new QLabel(tr("文字"), this);

    plainText1 = new QPlainTextEdit(this);
    plainText2 = new QPlainTextEdit(this);
    QFont font;
    font.setFamily("Courier New"); // 使用等宽字体
    plainText1->setFont(font);
    plainText2->setFont(font);
    plainText2->setReadOnly(true);
    plainText1->setPlaceholderText(tr("请输入待转换的十六进制字符串"));
    plainText2->setPlaceholderText(tr("转换结果将会出现在这里"));

    comboEncoding = new QComboBox(this);
    comboEncoding->addItem("ASCII", static_cast<int>(EncodingMode::ASCII));
    comboEncoding->addItem("GB2312", static_cast<int>(EncodingMode::GB2312));
    comboEncoding->setCurrentIndex(1);
    btnSwap = new QPushButton(QIcon(":/icon/交换.png"), tr("交换"), this);

    vLayout1->addWidget(label1);
    vLayout1->addWidget(plainText1);

    vLayout2->addStretch();
    vLayout2->addWidget(comboEncoding);
    vLayout2->addWidget(btnSwap);
    vLayout2->addStretch();

    vLayout3->addWidget(label2);
    vLayout3->addWidget(plainText2);

    mainLayout->addLayout(vLayout1);
    mainLayout->addLayout(vLayout2);
    mainLayout->addLayout(vLayout3);

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);

    connect(plainText1, &QPlainTextEdit::textChanged, this, &WindowHexConvert::onPlainText1Changed);
    connect(comboEncoding,
            &QComboBox::currentTextChanged,
            this,
            &WindowHexConvert::onEncodingChanged);
    connect(btnSwap, &QPushButton::clicked, this, &WindowHexConvert::onBtnSwapClicked);

    setWindowIcon(QIcon(":/icon/主页.png"));
}

WindowHexConvert::~WindowHexConvert() {}

void WindowHexConvert::onPlainText1Changed()
{
    doConvert();
}

void WindowHexConvert::onEncodingChanged()
{
    doConvert();
}

void WindowHexConvert::onBtnSwapClicked()
{
    QString text = label1->text();
    label1->setText(label2->text());
    label2->setText(text);

    isSwaped = !isSwaped;

    if (isSwaped) {
        setWindowTitle(tr("文字转十六进制字符串"));
    } else {
        setWindowTitle(tr("十六进制字符串转文字"));
    }

    doConvert();
}

void WindowHexConvert::doConvert()
{
    QString text = plainText1->toPlainText();
    EncodingMode mode = static_cast<EncodingMode>(comboEncoding->currentData().toInt());
    QString result;
    if (isSwaped) {
        // 文字转为十六进制字符串
        switch (mode) {
        case EncodingMode::ASCII:
            result = HexUtil::qStringToAsciiHexWithSpaces(text);
            break;
        case EncodingMode::GB2312:
            result = HexUtil::qStringToGb2312HexWithSpaces(text);
            break;
        }
    } else {
        // 十六进制字符串转为文字
        if (HexUtil::isValidHexString(text)) {
            auto bytes = HexUtil::hexStringToByteArray(text);
            result = HexUtil::convertByteArrayToString(bytes, mode);
        } else {
            emit showMessageInStatusBar(
                tr("输入的十六进制字符串无效，请重新输入，注意使用空格分割十六进制"));
        }
    }

    plainText2->setPlainText(result);
}
